﻿/*
 * Created by SharpDevelop.
 * User: lpelegrini
 * Date: 23/06/2013
 * Time: 11:44
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections;
using MySql.Data.MySqlClient;

namespace Fazenda.DAL
{
	/// <summary>
	/// Description of DataBaseHelper.
	/// </summary>
	public class DatabaseHelper
	{
		
		#region Propriedades
		
		public MySqlConnection MyBdConnection { get; set; }
		public string NomeStringConexao { get; set; }
		
		#endregion
		
		#region Construtores
		
		public DatabaseHelper()
		{
			this.NomeStringConexao = "Server=localhost;Database=Cadastro;Uid=root;Pwd=xxxxx;Connect Timeout=30;";
			this.MyBdConnection = new MySqlConnection(this.NomeStringConexao);
		}
		
		public DatabaseHelper(string nomeStringConexao)
		{
			
			this.NomeStringConexao = nomeStringConexao;
			this.MyBdConnection = new MySqlConnection(this.NomeStringConexao);
		}
		
		#endregion
		
		#region Métodos Privados
		
		private string GetCorrectParameterName(string parameterName)
		{
			if (parameterName[0] != '@')
			{
				parameterName = "@" + parameterName;
			}
			return parameterName;
		}
		
		#endregion
		
		#region Métodos Públicos
		
		public static DatabaseHelper Create()
		{
			return new DatabaseHelper();
		}
		
		public static DatabaseHelper Create(string nomeStringConexao)
		{
			return new DatabaseHelper(nomeStringConexao);
		}
		
		public void OpenConnection()
		{
			if (this.MyBdConnection.State == System.Data.ConnectionState.Closed)
			{
				this.MyBdConnection.Open();
			}
		}
		
		public void CloseConection()
		{
			this.MyBdConnection.Close();
		}
		/*
		public MySqlParameter BuildParameter(string nome, object valor, DbType tipo, int size)
		{
			MySqlParameter parametro = new MySqlParameter(this.GetCorrectParameterName(nome), valor);
			parametro.DbType = tipo;
			parametro.Size = size;
			return parametro;
		}
		
		public void BuildParameter(string nome, object valor, DbType tipo, int size, List listParametros)
		{
			MySqlParameter parametro = this.BuildParameter(nome, valor, tipo, size);
			listParametros.Add(parametro);
		}
		
		public MySqlParameter BuildOutPutParameter(string nome, DbType tipo, int size)
		{
			MySqlParameter parametro = new MySqlParameter();
			parametro.ParameterName = this.GetCorrectParameterName(nome);
			parametro.DbType = tipo;
			parametro.Size = size;
			parametro.Direction = ParameterDirection.Output;
			return parametro;
		}
		
		public void BuildOutPutParameter(string nome, DbType tipo, int size, List listParametros)
		{
			MySqlParameter parametro = this.BuildOutPutParameter(nome, tipo, size);
			listParametros.Add(parametro);
		}
		*/
		public void ExecuteNonQuery(MySqlCommand command)
		{
			command.ExecuteNonQuery();
		}
		
		public void ExecuteNonQuery(MySqlCommand command, bool openConnection)
		{
			if (openConnection)
			{
				this.OpenConnection();
			}
			this.ExecuteNonQuery(command);
			if (openConnection)
			{
				this.CloseConection();
			}
		}
		
		public void ExecuteNonQuery(string query, params MySqlParameter[] parameters)
		{
			Exception erro = null;
			try
			{
				this.OpenConnection();
				MySqlCommand command = this.MyBdConnection.CreateCommand();
				command.CommandText = query;
				command.Parameters.AddRange(parameters);
				this.ExecuteNonQuery(command);
				this.CloseConection();
			}
			catch (Exception ex)
			{
				erro = ex;
			}
			finally
			{
				this.CloseConection();
			}
			
			if (erro != null)
			{
				throw erro;
			}
		}
		
		public void ExecuteCommands(params MySqlCommand[] commands)
		{
			Exception erro = null;
			MySqlTransaction trans = null;
			try
			{
				this.MyBdConnection.Open();
				trans = this.MyBdConnection.BeginTransaction();
				for (int i = 0; i < commands.Length; i++)
				{
					commands[i].Transaction = trans;
					this.ExecuteNonQuery(commands[i]);
				}
				trans.Commit();
				this.MyBdConnection.Close();
			}
			catch(Exception ex)
			{
				trans.Rollback();
				erro = ex;
			}
			finally
			{
				this.MyBdConnection.Close();
			}
			
			if (erro != null)
			{
				throw erro;
			}
		}
		
		#endregion
	}
	
}